//
//  NSString+Extentions.m
//  WestAir
//
//  Created by Henry on 16/8/17.
//  Copyright © 2016年 Henry. All rights reserved.
//

#import "NSString+Extensions.h"

@implementation NSString (Extensions)

- (BOOL)isValidWithRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:self];
}

- (BOOL)isMobile{
    if (self.length != 11)
    {
        NSLog(@"手机号长度只能是11位");
        return NO;
    }
    
    if (![self hasPrefix:@"1"]) {
        NSLog(@"手机号只能是1开头");
        return NO;
    }
    return YES;
}

- (BOOL)isIDCard
{
    BOOL result = [self isValidWithRegex:CARDREGULAR];
    return result;
}

- (NSDate *)toDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:self];
    return date;
}

- (NSString *)toSafe
{
    NSString *first = [self substringToIndex:3];
    NSString *last = [self substringFromIndex:7];
    return [NSString stringWithFormat:@"%@****%@",first,last];
}

- (CGFloat)heightWithFontSize:(float)fontSize andMaxWidth:(float)theWidth
{
    UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize size = CGSizeMake(theWidth,2000);
    NSDictionary *attributes = @{NSFontAttributeName:font};
    CGRect rect = [self boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return rect.size.height;
}

@end
